package com.kaibes.core.base.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;

import com.kaibes.core.base.core.VerificationImage;

public class VerificationImageUtils {

    public static int WIDTH = 150;// 图片宽
    public static int HEIGHT = 35;// 图片高
    private static int LINES = 10;// 干扰线数量
    // 字体大小
    private static final int FONT_SIZE = 30;
    
    private static Random random = new Random();
    private static final char[] CHARS = {
            '1', '2', '3', '4', '5', '6', '7', '8', '9',
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
            'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
            'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
    private static final char[] NUMS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    private static final char[] LETTERS = {
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
            'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
            'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
    
    private static Color getRandomColor() {
        return new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
    }
    
    public static VerificationImage drawNums(int size) {
        // 1.创建空白图片
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_BGR);
        // 2.获取图片画笔
        Graphics graphic = image.getGraphics();// 产生Image对象的Graphics对象,改对象可以在图像上进行各种绘制操作
        // 3.设置画笔颜色
//        graphic.setColor(getRandomColor());//字体颜色
        graphic.setColor(Color.WHITE);
        // 4.绘制矩形背景
        graphic.fillRect(0, 0, WIDTH, HEIGHT);//图片大小
        
        // 绘制随机字符
        StringBuffer sb = new StringBuffer();
        for (int i = 1; i <= size; i++) {
            // 取随机字符索引
            int n = random.nextInt(NUMS.length);
            // 设置随机颜色
            graphic.setColor(getRandomColor());
            // 设置字体大小
            graphic.setFont(new Font(null, Font.BOLD + Font.ITALIC, FONT_SIZE));
//            new Font("Fixedsys", Font.CENTER_BASELINE, 18);
            // graphic.setFont(new Font("Times New Roman", Font.ROMAN_BASELINE, 18));//字体大小
            // 画字符
            char c = NUMS[n];
            graphic.drawString(String.valueOf(c), i * WIDTH / (size+1), HEIGHT*2/3);
            // 记录字符
            sb.append(c);
        }
        
        // 6.画干扰线
        for (int i = 0; i < LINES; i++) {
            // 设置随机颜色
            graphic.setColor(getRandomColor());
            // 随机画线
            graphic.drawLine(random.nextInt(WIDTH), random.nextInt(HEIGHT), random.nextInt(WIDTH), random.nextInt(HEIGHT));
        }
        graphic.dispose();
        
        VerificationImage verificationImage = new VerificationImage();
        verificationImage.setContent(sb.toString());
        verificationImage.setImage(image);
        return verificationImage;
    }
    
    public static VerificationImage drawLetters(int size) {
        // 1.创建空白图片
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_BGR);
        // 2.获取图片画笔
        Graphics graphic = image.getGraphics();// 产生Image对象的Graphics对象,改对象可以在图像上进行各种绘制操作
        // 3.设置画笔颜色
//        graphic.setColor(getRandomColor());//字体颜色
        graphic.setColor(Color.WHITE);
        // 4.绘制矩形背景
        graphic.fillRect(0, 0, WIDTH, HEIGHT);//图片大小
        
        // 绘制随机字符
        StringBuffer sb = new StringBuffer();
        for (int i = 1; i <= size; i++) {
            // 取随机字符索引
            int n = random.nextInt(LETTERS.length);
            // 设置随机颜色
            graphic.setColor(getRandomColor());
            // 设置字体大小
            graphic.setFont(new Font(null, Font.BOLD + Font.ITALIC, FONT_SIZE));
//            new Font("Fixedsys", Font.CENTER_BASELINE, 18);
            // graphic.setFont(new Font("Times New Roman", Font.ROMAN_BASELINE, 18));//字体大小
            // 画字符
            char c = LETTERS[n];
            graphic.drawString(String.valueOf(c), i * WIDTH / (size+1), HEIGHT*2/3);
            // 记录字符
            sb.append(c);
        }
        
        // 6.画干扰线
        for (int i = 0; i < LINES; i++) {
            // 设置随机颜色
            graphic.setColor(getRandomColor());
            // 随机画线
            graphic.drawLine(random.nextInt(WIDTH), random.nextInt(HEIGHT), random.nextInt(WIDTH), random.nextInt(HEIGHT));
        }
        VerificationImage verificationImage = new VerificationImage();
        verificationImage.setContent(sb.toString());
        verificationImage.setImage(image);
        return verificationImage;
    }
    
    public static VerificationImage drawChars(int size) {
        // 1.创建空白图片
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_BGR);
        // 2.获取图片画笔
        Graphics graphic = image.getGraphics();// 产生Image对象的Graphics对象,改对象可以在图像上进行各种绘制操作
        // 3.设置画笔颜色
//        graphic.setColor(getRandomColor());//字体颜色
        graphic.setColor(Color.WHITE);
        // 4.绘制矩形背景
        graphic.fillRect(0, 0, WIDTH, HEIGHT);//图片大小
        
        // 绘制随机字符
        StringBuffer sb = new StringBuffer();
        for (int i = 1; i <= size; i++) {
            // 取随机字符索引
            int n = random.nextInt(CHARS.length);
            // 设置随机颜色
            graphic.setColor(getRandomColor());
            // 设置字体大小
            graphic.setFont(new Font(null, Font.BOLD + Font.ITALIC, FONT_SIZE));
//            new Font("Fixedsys", Font.CENTER_BASELINE, 18);
            // graphic.setFont(new Font("Times New Roman", Font.ROMAN_BASELINE, 18));//字体大小
            // 画字符
            char c = CHARS[n];
            graphic.drawString(String.valueOf(c), i * WIDTH / (size+1), HEIGHT*2/3);
            // 记录字符
            sb.append(c);
        }
        
        // 6.画干扰线
        for (int i = 0; i < LINES; i++) {
            // 设置随机颜色
            graphic.setColor(getRandomColor());
            // 随机画线
            graphic.drawLine(random.nextInt(WIDTH), random.nextInt(HEIGHT), random.nextInt(WIDTH), random.nextInt(HEIGHT));
        }
        VerificationImage verificationImage = new VerificationImage();
        verificationImage.setContent(sb.toString());
        verificationImage.setImage(image);
        return verificationImage;
    }

}
